package org.macdadi.ks.client.components;

import org.macdadi.core.client.auth.AuthenticatedUser;
import org.macdadi.core.client.auth.AuthorizationException;
import org.macdadi.core.client.domain.DataService;
import org.macdadi.core.client.domain.DataWriteService;
import org.macdadi.core.client.domain.ProjectInfo;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.core.RegionPosition;
import com.gwtext.client.data.SimpleStore;
import com.gwtext.client.data.StringFieldDef;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.TabPanel;
import com.gwtext.client.widgets.Toolbar;
import com.gwtext.client.widgets.ToolbarButton;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.TextArea;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.grid.ColumnConfig;
import com.gwtext.client.widgets.grid.ColumnModel;
import com.gwtext.client.widgets.grid.EditorGridPanel;
import com.gwtext.client.widgets.grid.GridEditor;
import com.gwtext.client.widgets.layout.BorderLayout;
import com.gwtext.client.widgets.layout.BorderLayoutData;

public class ObjectDetails extends Composite {

	protected VerticalPanel userProfile;
	protected ProjectInfo project;
	protected String profileImageURL = "";
	protected String projectURL = "<p><b><big>Current Projects</p></b></big> <hr />";
	protected Panel faceImage;
	protected Panel bioText;
	protected int header_index = 1;
	protected String objectDetails;
	protected EditorGridPanel grid;
	protected TextField userInfoTF;
	final Panel projectField;
	final int numFields = 7;

	public ObjectDetails(final int id, final String object, final String authToken) {
		userProfile = new VerticalPanel();

		final Panel vspace = new Panel();
		vspace.setHeight(20);
		vspace.setBorder(false);

		Panel vspace_mini = new Panel();
		vspace_mini.setHeight(10);
		vspace_mini.setBorder(false);

		userProfile.setBorderWidth(10);
		userProfile.setHeight("600");

		final Panel faceImage = new Panel();
		faceImage.setAutoWidth(false);
		faceImage.setAutoHeight(false);
		faceImage.setHeight(200);
		faceImage.setWidth(200);
		profileImageURL = "";
		DataService.App.getInstance().getObjectPicture(id, object, authToken, new AsyncCallback() {
			public void onFailure(Throwable caught) {
				GWT.log("Failed to populate the projects table", caught);
				if (caught instanceof AuthorizationException) {
					System.out.println("Please login to view projects");
				}
			}

			public void onSuccess(Object result) {
				profileImageURL = (String) result;
				if(profileImageURL == null)
					profileImageURL = "http://www.littleengine.com/images/other/default-avatar.gif"; //DEFAULT PICTURE
				String URL = "<img src = " + "'" + profileImageURL + "'" + " />";
				faceImage.setHtml(URL);		
			}
		});
		userProfile.add(faceImage);
		
		Toolbar editPicBar = new Toolbar();
		editPicBar.setWidth(200);

		ToolbarButton editPicture = new ToolbarButton("Edit Picture"); 
		editPicture.setCls("macdadi-myaccount-editPicButton");
		editPicture.setTooltip("<p>Edit Picture</p>");
		final Window urlField = new Window();
		urlField.setTitle("Please enter image URL:");
		urlField.setFrame(true);
		urlField.setWidth(400);
		urlField.setHeight(120);
		urlField.setPaddings(20);
		
		final TextField tf = new TextField("URL: ", "urlField", 300);
		tf.setSelectOnFocus(true);
		tf.setValue("http://");
		urlField.add(tf);
		Button submitURL = new Button("Submit", new ButtonListenerAdapter() {
			public void onClick(Button button, EventObject e) {
				profileImageURL = tf.getText();
				updateProfileImage(id, object, profileImageURL, authToken);
				urlField.hide();
			}
		});
		urlField.add(submitURL);
		editPicture.addListener(new ButtonListenerAdapter() {
			public void onClick(Button button, EventObject e) {
				urlField.show();
			}
		});
		editPicBar.addButton(editPicture);
		userProfile.add(editPicBar);
		
		projectField = new Panel();
		projectField.setHeight(190);
		projectField.setWidth(200);
		projectField.setBorder(true);
		projectField.setAutoScroll(true);
		
		if(object.equals("goal")) {
			projectField.setBodyStyle("background-image: url( 'http://photos-a.ak.fbcdn.net/hphotos-ak-snc1/hs021.snc1/3051_1123090394652_1148850067_30636928_2898851_n.jpg' )");
		}
		if(object.equals("option")) {
			projectField.setBodyStyle("background-image: url( 'http://photos-h.ak.fbcdn.net/hphotos-ak-snc1/hs021.snc1/3051_1123090354651_1148850067_30636927_926572_n.jpg' )");
		}
		
		DataService.App.getInstance().getObjectDetails(id, object, authToken, new AsyncCallback() {
			public void onFailure(Throwable caught) {
				GWT.log("Failed to populate the info table", caught);
				if (caught instanceof AuthorizationException) {
					System.out.println("Please login to view projects");
				}
			}

			public void onSuccess(Object result) {
				objectDetails = (String) result;
				projectField.setHtml("<p><font size = 3>" + objectDetails + "</font></p>");
			}
		});
		grid = new EditorGridPanel();
		setUpGrid(grid, numFields);
		userProfile.add(grid);
		userProfile.add(projectField);
		initWidget(userProfile);
	}

	private void updateProfileImage(int id, String object, String profileImageURL, String authToken) {
		faceImage.clear();
		String URL = "<img src = " + "'" + profileImageURL + "'" + " />";
		faceImage.setHtml(URL);
		DataWriteService.App.getInstance().saveObjectPicture(id, profileImageURL, authToken, new AsyncCallback() {
			public void onFailure(Throwable caught) {
				GWT.log("Failed to populate the projects table", caught);
				if (caught instanceof AuthorizationException) {
					System.out.println("Please login to view projects");
				}
			}
			public void onSuccess(Object result) {
				System.out.println("Image Save Successful");
			}
		});
	}

	public void saveDetails(int id, String text, String authToken) {
		DataWriteService.App.getInstance().saveObjectDetails(id, text, authToken, new AsyncCallback() {
			public void onFailure(Throwable caught) {
				GWT.log("Failed to populate the projects table", caught);
				if (caught instanceof AuthorizationException) {
					System.out.println("Please login to view projects");
				}
			}
			public void onSuccess(Object result) {
				System.out.println("Biography Save Successful");
			}
		});
	}

	private void setUpGrid(EditorGridPanel grid, int numFields) {
		String[] userInfo = new String[numFields];
		for(int i = 0; i < numFields; i++) {
			userInfo[i] = "TEST METRIC " + i;
		}
		SimpleStore dataStore = new SimpleStore(new String[]{"set", "info"}, new String[][] {
				new String[]{"3:", userInfo[0]},
				new String[]{"2:", userInfo[1]},
				new String[]{"1:", userInfo[2]},
				new String[]{"0:", userInfo[3]},
				new String[]{"-1:", userInfo[4]},
				new String[]{"-2:", userInfo[5]},
				new String[]{"-3:", userInfo[6]},
		});
		dataStore.load();
		StringFieldDef set = new StringFieldDef("set");
		StringFieldDef usrInfo = new StringFieldDef("info");
		ColumnConfig setCol = new ColumnConfig("", set.getName(), 70, true, null, "set");
		ColumnConfig infoCol = new ColumnConfig("", usrInfo.getName(), 110, true, null, "info");  
		infoCol.setEditor(new GridEditor(userInfoTF = new TextField()));

		ColumnConfig[] columnConfigs = {setCol, infoCol};  
		ColumnModel columnModel = new ColumnModel(columnConfigs);

		grid.setStore(dataStore);
		grid.setColumnModel(columnModel);  
		grid.setWidth(200);  
		grid.setTrackMouseOver(true);
		grid.setAutoHeight(true);
		grid.setTitle("Metric Information");  
		grid.setFrame(true);
		grid.setClicksToEdit(1);
	}
}
